package com.geek.service.impl;

import com.geek.mapper.MarketGrouponMapper;
import com.geek.mapper.MarketGrouponRulesMapper;
import com.geek.model.MarketGroupon;
import com.geek.model.MarketGrouponExample;
import com.geek.model.MarketGrouponRules;
import com.geek.model.MarketGrouponRulesExample;
import com.geek.service.GrouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wbw
 * @data 2024/06/17 22:22
 **/
@Service
public class GrouponServiceImpl implements GrouponService {
    @Autowired
    MarketGrouponMapper grouponMapper;
    @Autowired
    MarketGrouponRulesMapper grouponRulesMapper;

    @Override
    public MarketGroupon findById(Integer grouponResultId) {
        MarketGrouponExample example = new MarketGrouponExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andIdEqualTo(grouponResultId);
        List<MarketGroupon> marketGroupons = grouponMapper.selectByExample(example);
        return marketGroupons != null && marketGroupons.size() == 1 ? marketGroupons.get(0) : null;
    }

    @Override
    public MarketGrouponRules findByGroupRulesId(Integer rulesId) {
        MarketGrouponRulesExample example = new MarketGrouponRulesExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andIdEqualTo(rulesId);
        List<MarketGrouponRules> marketGrouponRules = grouponRulesMapper.selectByExample(example);
        MarketGrouponExample grouponExample = new MarketGrouponExample();
        grouponExample.createCriteria().andDeletedEqualTo(false)
                .andRulesIdEqualTo(rulesId);
        List<MarketGroupon> marketGroupons = grouponMapper.selectByExample(grouponExample);
        if (marketGroupons.size() > marketGrouponRules.get(0).getDiscountMember()){
            return marketGrouponRules != null && marketGrouponRules.size() == 1? marketGrouponRules.get(0) : null;
        }else {
            return null;
        }

    }
}
